package br.indt.framework.ui.common;

public class PopUpScreen implements MenuItemListener, Runnable {
	public static final int ERRO = 1;
	public static final int ALERT = 2;
	public static final int SUCCESS = 3;
	public static final int LOADING = 4;
	public static final int NOTE = 5;
	public static final int NOTIFICATION = 6;	

	public static final int TIME_POPUP = 3000; // 3 seconds

	private Form form  = null;
	private MenuItem rskItem = null;
	PopUpItem item =  new PopUpItemFullTouch();
	private long startPopUp = 0;
	private long endPopUp = 0;

	public PopUpScreen() {
		try {
			form = new Form("PopUpScreen");
			
			// Add PopUpItem
			PopUpBlankItem item0 = new PopUpBlankItem();
			form.add(item0, false);
			form.add(item, false);

			form.setList(false);
			form.setMenuList(true);
			form.setPopUp(true);
			
//			if (UIManager.isNextGeneration()) {
//				// to discard back button action in NG
//				form.addCommand(new Command("", Command.BACK, 0));
//			}			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void show(int pType, String pNote) {
		this.form.menuListShow = true;
		item.setType(pType);
		item.setNote(pNote);
		startPopUp = System.currentTimeMillis();

		try {
			Display.getInstance().pushLoading(form);

			if (pType == ERRO || pType == SUCCESS || pType == ALERT) {
				new Thread(this).start();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// from MenuItemListener
	public void menuItemClicked(MenuItem menuItem) {
		if (menuItem == rskItem) {
			Display.getInstance().setControllerListener(true);
			Display.getInstance().pop();
		}
	}

	public void run() {
		try {
			while((endPopUp-startPopUp) < TIME_POPUP) {
				endPopUp = System.currentTimeMillis();
				Thread.sleep(200);
			}

			Display.getInstance().setControllerListener(true);
			Display.getInstance().popLoading();

		} catch (Exception e) {
    		e.printStackTrace();
		}
	}
}
